package com.hzz.service.report;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.report.IReportTaskService;
import com.hzz.dao.report.IReportTaskDao;
import com.hzz.dao.report.IReportTaskSubmitDao;
import com.hzz.dao.user.IUserDao;
import com.hzz.dto.report.ReportTaskDto;
import com.hzz.entity.report.ReportTask;
import com.hzz.entity.report.ReportTaskSubmit;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.IdUtil;
import com.hzz.framework.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@FeignService
@Slf4j
public class ReportTaskService extends BaseService implements IReportTaskService {
    @Autowired
    IReportTaskDao reportTaskDao;
    @Autowired
    IReportTaskSubmitDao reportTaskSubmitDao;
    @Autowired
    IUserDao userDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public Response saveOrUpdateData(ReportTaskDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            //判断数据是否存在
            if (reportTaskDao.isDataExist(entity) != 0) {
                //数据存在
                reportTaskDao.update(entity);
            } else {
                //新增
                if (entity.getId() == null) {
                    entity.setId(IdUtil.nextId());
                }
                reportTaskDao.insert(entity);
                result.data = entity.getId();
                //查询机构下人员创建任务
                if (ValidatorUtil.notEmpty(entity.getTaskOrgId())) {
                    String[] taskOrgIdArr = entity.getTaskOrgId().split(",");
                    String[] taskOrgNameArr = entity.getTaskOrgName().split(",");
                    if (taskOrgIdArr != null && taskOrgIdArr.length > 0) {
                        List<ReportTaskSubmit> reportTaskSubmitList = new ArrayList<>();
                        for (int i = 0; i < taskOrgIdArr.length; i++) {
                            try {
                                String taskOrgId = taskOrgIdArr[i];
                                if (ValidatorUtil.isEmpty(taskOrgId)) {
                                    continue;
                                }
                                ReportTaskSubmit reportTaskSubmit = new ReportTaskSubmit();
                                reportTaskSubmit.setId(IdUtil.nextId());
                                reportTaskSubmit.setReportTaskId(entity.getId());
                                reportTaskSubmit.setTaskOrgId(Long.parseLong(taskOrgId));
                                reportTaskSubmit.setTaskOrgName(taskOrgNameArr[i]);
                                reportTaskSubmit.setCreateId(entity.getCreateId());
                                reportTaskSubmitList.add(reportTaskSubmit);
                            } catch (Exception e) {
                                log.error("创建任务!", e);
                            }
                        }
                        reportTaskSubmitDao.insertBatch(reportTaskSubmitList);
                    }
                }
            }
        } catch (Exception e) {
            log.error("信息保存异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public String deleteData(ReportTaskDto dto) throws Exception {
        String result = "success";
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            if (reportTaskDao.deleteByPrimaryKey(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("物理删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public String deleteDataById(ReportTaskDto dto) throws Exception {
        String result = "success";
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            if (reportTaskDao.deleteById(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("逻辑删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public PageInfo findDataIsPage(ReportTaskDto dto) throws Exception {
        PageInfo pageInfo = null;
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
            List list = reportTaskDao.findDataIsPage(entity);
            pageInfo = new PageInfo(list);
            pageInfo.setList(copyTo(pageInfo.getList(), ReportTaskDto.class));
        } catch (Exception e) {
            log.error("信息[分页]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return pageInfo;
    }

    @Override
    public List<ReportTaskDto> findDataIsList(ReportTaskDto dto) throws Exception {
        List<ReportTaskDto> results = null;
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            results = copyTo(reportTaskDao.findDataIsList(entity), ReportTaskDto.class);
        } catch (Exception e) {
            log.error("信息[列表]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return results;
    }

    @Override
    public ReportTaskDto findDataById(ReportTaskDto dto) throws Exception {
        ReportTaskDto result = null;
        try {
            ReportTask entity = copyTo(dto, ReportTask.class);
            result = copyTo(reportTaskDao.selectByPrimaryKey(entity), ReportTaskDto.class);
        } catch (Exception e) {
            log.error("信息[详情]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }


}
